// pages/active_detail/people_list/people_list.js
const app = getApp();
var router = require("../../../router.js");
var animate = require('../../../utils/animate.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show_confirm_box: false,

    active: {      
    },
    animationData: '',
    fadeIn: '',
    active_people: '',
    username: '',
    userid: '',
    userphone: '',
    show_tj: false,//是否显示推荐信息
    tj_info: { //推荐活动信息    
    },//推荐信息
  },
  change_name(e) {
    let v = e.detail.value;
    this.setData({
      username: v
    })
  },
  change_id(e) {
    let v = e.detail.value;
    this.setData({
      userid: v
    })
  },
  change_phone(e) {
    let v = e.detail.value;
    this.setData({
      userphone: v
    })
  },
  get_info() {
    wx.getStorage({
      key: 'expand_list',
      success: (res) => {  
        console.log(res)           
        if (res) {          
          this.setData({
            active: res.data
          })
        }        
      },
    });   
  },
  go_detail() {  //了解更多 跳转 活动详情
    app.navTo('../../active_detail/active_detail?id=' + this.data.tj_info.id)
  },
  open_confirm_box() {
    if (!this.data.username || !this.data.userid || !this.data.userphone) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      });
      return;
    }    
    var that = this;
    app.request({
      url: router.expand.add,
      method: "POST",
      data: {
        user_id: wx.getStorageSync("userInfo")['id'],
        expand_id: that.data.active.id,
        name: that.data.username,
        id_card: that.data.userid,
        phone: that.data.userphone,        
      },
      success: function (data) {    
        console.log(data);
        //return false;
        if (data.status == true) {
          wx.showToast({
            title: '操作成功',
          });
          that.setData({
            pay_price: data.price
          });      
          if (data.list.id){
            that.setData({
              tj_info: { //推荐活动信息
                id: data.list.id,//标题
                title: data.list.title,//标题
                des: data.list.content,
                date: data.list.add_time,
                price: data.list.price,//价格
                img: data.list.banner
              },
            });       
          }        
          if (data.price > 0 && data.info) {          
            that.setData({
              pay_info: data.info,
              show_confirm_box: true
            });
            let animation = wx.createAnimation({
              duration: '300',
              timingFunction: 'ease',
            })
            animation.translate(0, -50).opacity(1).step()
            that.setData({
              animationData: animation.export()
            })
            let animation2 = wx.createAnimation({
              duration: '300',
              timingFunction: 'ease',
            })
            animation2.opacity(1).step()
            that.setData({
              fadeIn: animation2.export()
            })
          } else {
            that.showInfo();
          } 
        } else {
          wx.showToast({
            title: '操作失败',
            icon: 'none',
            mask: true,
            duration: 2000
          });
        }
      }
    });
  },
  pay: function (data) {
    var payInfo = this.data.pay_info;
    if (!payInfo) {
      wx.showToast({
        title: '支付失败',
        icon: 'none'
      });
      return false;
    }
    var that = this;
    wx.requestPayment({
      'timeStamp': payInfo.timeStamp,
      'nonceStr': payInfo.nonceStr,
      'package': payInfo.package,
      'signType': payInfo.signType,
      'paySign': payInfo.paySign,
      'success': function (res) {
        console.log(res.errMsg)
        if (res.errMsg == 'requestPayment:ok') {
          wx.showToast({
            title: '支付成功',
            icon: 'none'
          })
          that.setData({
            show_confirm_box: false
          });
          that.showInfo();
        }
      },
      'fail': function (res) {
        console.log(res.errMsg)
        if (res.errMsg == 'requestPayment:fail cancel') {
          wx.showToast({
            title: '取消支付',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '支付失败',
            icon: 'none'
          })
        }
      },
      'complete': function (res) {
      }
    })
  },
  showInfo: function () {
    if (this.data.tj_info.id) {
      this.setData({
        show_tj: true,
      });
      animate.fade_up(this);
    } else {
      app.navTo('../../my_bm/my_bm');
    }
  },
  clsoe_confirm_box() {
    let animation = wx.createAnimation({
      duration: '300',
      timingFunction: 'ease',
    })
    animation.translate(0, 0).opacity(0).step()
    this.setData({
      animationData: animation.export()
    })
    let animation2 = wx.createAnimation({
      duration: '300',
      timingFunction: 'ease',
    })
    animation2.opacity(0).step()
    this.setData({
      fadeIn: animation2.export()
    })
    setTimeout(() => {
      this.setData({
        show_confirm_box: false
      });     
    }, 300)
    this.showInfo();
  },
  to_add_people() {
    wx.navigateTo({
      url: '../add_people/add_people',
    })
  },
  confirm_active_info() {
    // 打开确认信息窗口，
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.get_info() // 获取活动信息及人员信息
  }
})